
package com.hlkj.pay.service.ad.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.pay.common.constants.RedisConstants;
import com.hlkj.pay.dto.ad.query.AdInfoQueryDto;
import com.hlkj.pay.infrastructure.mapper.ad.AdInfoConfigMapper;
import com.hlkj.pay.infrastructure.mapper.ad.AdInfoMapper;
import com.hlkj.pay.infrastructure.mapper.ad.AdStatisticsMapper;
import com.hlkj.pay.infrastructure.model.ad.AdInfoConfigDO;
import com.hlkj.pay.infrastructure.model.ad.AdInfoDO;
import com.hlkj.pay.service.ad.IAdInfoService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/22 18:24
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdInfoServiceImpl implements IAdInfoService {

    private final AdInfoMapper adInfoMapper;

    private final AdInfoConfigMapper adInfoConfigMapper;
    private final AdStatisticsMapper adStatisticsMapper;

    @Override
    public PageResult<AdInfoDO> queryPage(AdInfoQueryDto adInfoQueryDto) {
        return adInfoMapper.selectPage(adInfoQueryDto, buildWrapper(adInfoQueryDto));
    }

    @Override
    @Cacheable(value = RedisConstants.AD_INFO, key = "#id", unless = "#result == null")
    public AdInfoDO queryById(Long id) {
        return adInfoMapper.selectById(id);
    }

    @Override
    public void saveAd(AdInfoDO adInfoDO) {
        adInfoMapper.insert(adInfoDO);
    }

    @Override
    @CacheEvict(value = RedisConstants.AD_INFO, key = "#udpateAdInfoDO.id")
    public void updateAd(AdInfoDO udpateAdInfoDO) {
        adInfoMapper.updateById(udpateAdInfoDO);
    }

    @Override
    @CacheEvict(value = RedisConstants.AD_INFO, key = "#id")
    public void delAd(Long id) {
        adInfoMapper.deleteById(id);
    }

    @Override
    @CacheEvict(value = RedisConstants.AD_INFO, key = "#id")
    public void updateAdStatus(Long id, Integer status) {
        AdInfoDO adInfoDO = new AdInfoDO();
        adInfoDO.setStatus(status);
        adInfoDO.setId(id);
        adInfoMapper.updateById(adInfoDO);
    }

    @Override
    @Cacheable(value = RedisConstants.AD_CONFIG, key = "#id", unless = "#result == null")
    public AdInfoConfigDO queryConfigByAdId(Long id) {
        return adInfoConfigMapper.selectOne(AdInfoConfigDO::getAdId, id);
    }

    @Override
    public void saveConfig(AdInfoConfigDO adInfoConfigDO) {
        adInfoConfigMapper.insert(adInfoConfigDO);
    }

    @Override
    @CacheEvict(value = RedisConstants.AD_CONFIG, key = "#adInfoConfigDO.adId")
    public void updateConfig(AdInfoConfigDO adInfoConfigDO) {
        adInfoConfigMapper.updateById(adInfoConfigDO);
    }

    @Override
    public void updateStatistics(AdInfoDO adInfoDO) {
        adStatisticsMapper.updateAdTotalStatistics(adInfoDO);
    }

    /**
     * 查询条件封装
     *
     * @param adInfoQueryDto
     * @return
     */
    LambdaQueryWrapper<AdInfoDO> buildWrapper(AdInfoQueryDto adInfoQueryDto) {
        LambdaQueryWrapperX<AdInfoDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(AdInfoDO::getMediaId, adInfoQueryDto.getMediaId());
        queryWrapper.eqIfPresent(AdInfoDO::getType, adInfoQueryDto.getType());
        queryWrapper.eqIfPresent(AdInfoDO::getName, adInfoQueryDto.getName());
        queryWrapper.eqIfPresent(AdInfoDO::getStatus, adInfoQueryDto.getStatus());
        queryWrapper.likeIfPresent(AdInfoDO::getName, adInfoQueryDto.getLikeName());
        queryWrapper.geIfPresent(AdInfoDO::getCreateTime, adInfoQueryDto.getStartTime());
        queryWrapper.leIfPresent(AdInfoDO::getCreateTime, adInfoQueryDto.getEndTime());

        return queryWrapper;
    }
}
